﻿<common:LayoutAwarePage
    x:Class="mPassRT.Pages.DatabasesPage"
    x:Name="pageRoot"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:common="using:mPassRT.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:converters="using:mPassRT.Converters"
    xmlns:selectors="using:mPassRT.Selectors"
    mc:Ignorable="d">
    <Page.Resources>
        <x:String x:Key="SubHeaderText">Password Databases</x:String>
        <x:Double x:Key="IconSize">110</x:Double>
        <Thickness x:Key="IconMargin">10</Thickness>
        
        <!-- Grid-appropriate 500 by 130 pixel item template as seen in the GroupDetailPage -->
        <DataTemplate x:Key="DatabaseItemTemplate">
            <Grid Height="110" Width="480" Margin="{StaticResource IconMargin}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="{StaticResource IconSize}" Height="{StaticResource IconSize}">
                    <TextBlock Style="{StaticResource CommonIconStyle}" Text="&#xE1F6;"
                               HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
                <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
                    <TextBlock Text="{Binding Name}" Style="{StaticResource ItemPrimaryPropertyStyle}" TextWrapping="NoWrap"/>
                    <TextBlock Text="This PC" Style="{StaticResource ItemSecondaryPropertyStyle}" TextWrapping="NoWrap"/>
                </StackPanel>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="NewItemTemplate">
            <Grid Height="110" Width="480" Margin="{StaticResource IconMargin}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border Background="Transparent" Width="{StaticResource IconSize}" Height="{StaticResource IconSize}">
                    <TextBlock Style="{StaticResource CommonIconStyle}" Text="&#xE0C5;"
                               HorizontalAlignment="Center" VerticalAlignment="Center"  />
                </Border>
                <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                    <TextBlock Style="{StaticResource ItemPrimaryPropertyStyle}" TextWrapping="NoWrap" VerticalAlignment="Center"/>
                </StackPanel>
            </Grid>
        </DataTemplate>

        <converters:BooleanToVisibilityConverter x:Key="VisibilityConverter"/>
        
        <selectors:TemplateSelector x:Key="TemplateSelector">
            <selectors:TemplateSelector.Mappings>
                <selectors:TemplateSelectorMapping TypeName="DatabaseViewModel" Template="{StaticResource DatabaseItemTemplate}"/>
                <selectors:TemplateSelectorMapping TypeName="NewItemViewModel" Template="{StaticResource NewItemTemplate}"/>
            </selectors:TemplateSelector.Mappings>
        </selectors:TemplateSelector>
        
        <selectors:GenericStyleSelector x:Key="StyleSelector">
            <selectors:GenericStyleSelector.Mappings>
                <selectors:StyleSelectorMapping TypeName="NewItemViewModel" MappedStyle="{StaticResource GridViewAddButtonItemContainerStyle}"/>
            </selectors:GenericStyleSelector.Mappings>
        </selectors:GenericStyleSelector>
    </Page.Resources>
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Grid.Column="1" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}"/>
            <TextBlock Text="{StaticResource SubHeaderText}" Grid.Column="2" Style="{StaticResource PageSubheaderTextStyle}"/>
        </Grid>

        <GridView Grid.RowSpan="2" ItemsSource="{Binding Databases}" ItemTemplateSelector="{StaticResource TemplateSelector}" 
                  SelectionMode="Extended" Padding="116,137,40,46" IsItemClickEnabled="True" 
                  ItemClick="OnDatabasesGridViewItemClick" SelectionChanged="OnDatabasesGridViewSelectionChanged" ItemContainerStyleSelector="{StaticResource StyleSelector}">
        </GridView>
        
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <!-- apply different back button style -->
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
    
    <Page.BottomAppBar>
        <AppBar x:Name="appBar" IsSticky="True" Style="{StaticResource AppbarStyle}">
            <StackPanel Orientation="Horizontal">
                <Button Style="{StaticResource RemoveAppBarButtonStyle}" Command="{Binding RemoveCommand}" Visibility="{Binding AreDatabasesSelected, Converter={StaticResource VisibilityConverter}}"/>
            </StackPanel>
        </AppBar>
    </Page.BottomAppBar>
</common:LayoutAwarePage>
